#!/bin/bash

# 自动打包脚本

set -e

PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_DIR"

# 版本号自增逻辑
VERSION_FILE="VERSION"
if [ ! -f "$VERSION_FILE" ]; then
    echo "1.0.0" > "$VERSION_FILE"
fi

CURRENT_VERSION=$(cat "$VERSION_FILE")
echo "当前版本: $CURRENT_VERSION"

# 解析版本号
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}

# 自增补丁版本号
PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "$NEW_VERSION" > "$VERSION_FILE"
echo "新版本: $NEW_VERSION"

# 更新CMakeLists.txt中的版本号
if [ -f "CMakeLists.txt" ]; then
    sed -i "s/project(linux-resource-monitor VERSION .*)/project(linux-resource-monitor VERSION $NEW_VERSION)/" CMakeLists.txt
    echo "已更新 CMakeLists.txt 中的版本号"
fi

# 检查必要的构建工具
if ! command -v cmake &> /dev/null; then
    echo "错误: 未找到 cmake 命令"
    echo "请安装 cmake 后再运行此脚本"
    exit 1
fi

if ! command -v make &> /dev/null; then
    echo "错误: 未找到 make 命令"
    echo "请安装 make 后再运行此脚本"
    exit 1
fi

if ! command -v debuild &> /dev/null; then
    echo "错误: 未找到 debuild 命令"
    echo "请安装 devscripts 包后再运行此脚本"
    exit 1
fi

# 检查Qt5开发包
if ! pkg-config --exists Qt5Core Qt5Widgets; then
    echo "错误: 未找到 Qt5 开发包"
    echo "请安装 qtbase5-dev 包后再运行此脚本"
    exit 1
fi

# 创建必要的目录
mkdir -p debian/usr/share/icons/hicolor/48x48/apps
mkdir -p debian/usr/share/applications

# 复制图标和desktop文件到debian目录（如果存在）
if [ -f "resources/icons/linux-resource-monitor.png" ]; then
    cp resources/icons/linux-resource-monitor.png debian/usr/share/icons/hicolor/48x48/apps/
else
    # 创建一个占位符图标
    touch debian/usr/share/icons/hicolor/48x48/apps/linux-resource-monitor.png
fi

if [ -f "resources/linux-resource-monitor.desktop" ]; then
    cp resources/linux-resource-monitor.desktop debian/usr/share/applications/
else
    # 创建基本的desktop文件
    cat > debian/usr/share/applications/linux-resource-monitor.desktop << EOF
[Desktop Entry]
Name=Linux资源监控器
Comment=Monitor system resources and CPU scheduler
Exec=linux-resource-monitor
Icon=linux-resource-monitor
Terminal=false
Type=Application
Categories=System;Monitor;
X-GNOME-Autostart-enabled=true
X-KDE-autostart-after=panel
X-MATE-Autostart-Delay=5
EOF
fi

# 创建基本的资源文件结构
mkdir -p resources/icons
touch resources/icons/linux-resource-monitor.png

mkdir -p resources
cat > resources/linux-resource-monitor.desktop << EOF
[Desktop Entry]
Name=Linux资源监控器
Comment=Monitor system resources and CPU scheduler
Exec=linux-resource-monitor
Icon=linux-resource-monitor
Terminal=false
Type=Application
Categories=System;Monitor;
X-GNOME-Autostart-enabled=true
X-KDE-autostart-after=panel
X-MATE-Autostart-Delay=5
EOF

# 如果不存在 LICENSE 文件则创建一个
if [ ! -f "LICENSE" ]; then
    echo "MIT License" > LICENSE
    echo "Copyright (c) 2023" >> LICENSE
    echo "" >> LICENSE
    echo "Permission is hereby granted..." >> LICENSE
fi

# 更新debian/changelog文件中的版本号
CURRENT_DATE=$(date -R)
cat > debian/changelog << EOF
linux-resource-monitor ($NEW_VERSION) unstable; urgency=medium

  * New upstream version $NEW_VERSION

 -- Your Name <your.email@example.com>  $CURRENT_DATE

EOF

# 构建项目
mkdir -p build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr
make -j$(nproc)

# 创建安装目录
make DESTDIR="$PROJECT_DIR/debian" install

# 返回项目根目录
cd "$PROJECT_DIR"

# 构建deb包
debuild -us -uc -b

echo "Debian package built successfully!"
echo "Package location: ../linux-resource-monitor_*.deb"